//
//  QuartzFunView.m
//  iPhoneDev01
//
//  Created by smu on 11-7-14.
//  Copyright 2011年 duk. All rights reserved.
//

#import "QuartzFunView.h"
#import "UIColor-Random.h"


@implementation QuartzFunView
@synthesize firstTouch;
@synthesize lastTouch;
@synthesize currentColor;
@synthesize shapeType;
@synthesize drawImage;
@synthesize useRandomColor;
@synthesize redrawRect;
@synthesize currentRect;
//当前区域，用于优化应用程序
- (CGRect)currentRect
{
    return CGRectMake(
                      (firstTouch.x > lastTouch.x) ? lastTouch.x : firstTouch.x,
                      (firstTouch.y > lastTouch.y) ? lastTouch.y : firstTouch.y,
                      fabsf(firstTouch.x - lastTouch.x),
                      fabsf(firstTouch.y - lastTouch.y));
}
//该视图是从nib中加载的，因此我们首先实现initWithCoder:请记住nib中的对象实例
//将存储为归档对象
- (id)initWithCoder:(NSCoder *)coder           
{
    if ((self = [super initWithCoder:coder])) {
        self.currentColor = [UIColor redColor];
        self.useRandomColor = NO;
        if (drawImage == nil) {
            self.drawImage = [UIImage imageNamed:@"iphone.png"];
        }
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing 
    //检索对当前上下文的引用，以便知道要绘图的位置
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //将线宽设置为2.0,这意味着我们画的任何直线都是2个像素
    CGContextSetLineWidth(context, 2.0);
    //设置所画直线的颜色,UIColor有该方法所需的CGColor属性
    CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
    //currentColor设置为填充颜色
    CGContextSetFillColorWithColor(context, currentColor.CGColor);
    //用户拖动描述的矩形
    //两个中较小的x值和较小的y值来创建起点,然后通过获得两个x值和两个y值之差的绝对值来计算出大小
    CGRect currentRect = CGRectMake (
                                     (firstTouch.x > lastTouch.x) ? lastTouch.x : firstTouch.x,
                                     (firstTouch.y > lastTouch.y) ? lastTouch.y : firstTouch.y,
                                     fabsf(firstTouch.x - lastTouch.x),
                                     fabsf(firstTouch.y - lastTouch.y));
    //switch跳转到每个形状类型的相应代码
    switch (shapeType) {
        case kLineShape:
            //不可见画笔移动到用户触摸的第一个位置
            CGContextMoveToPoint(context, firstTouch.x, firstTouch.y);
            //绘制一条从该点到用户触摸的最后一个点的直线
            CGContextAddLineToPoint(context, lastTouch.x, lastTouch.y);
            //画出这条路径
            CGContextStrokePath(context);
            break;
        case kRectShape:  
            CGContextAddRect(context, currentRect);  //绘制矩形
            CGContextDrawPath(context, kCGPathFillStroke); //绘画并填充它
            break;
        case kEllipseShape: 
            CGContextAddEllipseInRect(context, currentRect); //定义的矩形中绘制椭圆形
            CGContextDrawPath(context, kCGPathFillStroke);
            break;
        case kImageShape: 
        { //添加花括号：告诉GCC停止抱怨的一种方式
            CGFloat horizontalOffset = drawImage.size.width / 2;
            CGFloat verticalOffset = drawImage.size.height / 2;
            //绘制的图像以用户上次触摸的点为中心
            CGPoint drawPoint = CGPointMake(lastTouch.x - horizontalOffset,
                                            lastTouch.y - verticalOffset);
            //通知图像绘制自身
            [drawImage drawAtPoint:drawPoint];            
            break;
        }
        default:
            break;
    }
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //如果用户已经使用之前添加到UIColor中的新randomColor方法选择了某个随机颜色，
    //则我们需要更改此颜色
    if (useRandomColor) {
        self.currentColor = [UIColor randomColor];
    }
    UITouch *touch = [touches anyObject];
    firstTouch = [touch locationInView:self];
    lastTouch = [touch locationInView:self];
    //通过self上调用setNeedsDisplay来重新绘制视图
    [self setNeedsDisplay];
    
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    lastTouch = [touch locationInView:self];
    
    //联合当前矩形和上次矩形
    if (shapeType == kImageShape) {
        CGFloat horizontalOffset = drawImage.size.width  / 2.0;
        CGFloat verticalOffset = drawImage.size.height / 2.0;
        redrawRect = CGRectUnion(redrawRect, CGRectMake(lastTouch.x - horizontalOffset,
                                                        lastTouch.y - verticalOffset,
                                                        drawImage.size.width, 
                                                        drawImage.size.height));
    } else {
        redrawRect = CGRectUnion(redrawRect, self.currentRect);
    }
    redrawRect = CGRectInset(redrawRect, -2.0, -2.0);
    //将视图区域第一个矩形部分标记为需要重新显示
    [self setNeedsDisplayInRect:redrawRect];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    lastTouch = [touch locationInView:self];
    
    if (shapeType == kImageShape) {
        CGFloat horizontalOffset = drawImage.size.width / 2.0;
        CGFloat verticalOffset = drawImage.size.height / 2.0;
        redrawRect = CGRectUnion(redrawRect, CGRectMake(lastTouch.x - horizontalOffset, 
                                                        lastTouch.y - verticalOffset, 
                                                        drawImage.size.width,
                                                        drawImage.size.height));
    }
    else
        redrawRect = CGRectUnion(redrawRect, self.currentRect);
    redrawRect = CGRectInset(redrawRect, -2.0, -2.0);
    [self setNeedsDisplayInRect:redrawRect];
}
- (void)dealloc
{
    [currentColor release];
    [drawImage release];
    [super dealloc];
}

@end
